package com.taptech.doufu.ui.activity;

import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.util.AppUtil;
import com.taptech.doufu.ui.adapter.MoreBannerAdapter;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.bean.ResponseMoreBannerBean;
import com.taptech.doufu.bean.cartoon.ResponseMoreBannerCartoonBean;
import com.taptech.doufu.net.retrofit.api.ApiServer;
import com.taptech.doufu.util.DoufuUtils;
import com.taptech.doufu.util.ItemClickSupport;

import java.util.List;

import rx.Observer;

/**
 * 更多 for 征文活动
 */

public class MoreBannerActivity extends DiaobaoBaseActivity {

    private static final String TAG = MoreBannerActivity.class.getSimpleName();

    private RecyclerView mMoreBannerList;
    private MoreBannerAdapter moreBannerAdapter;
    private List<DFHomeBannerBean> mDFHomeBannerBeanList;
    private TextView mTitle;


    private ApiServer cpService;

    private String more_url;
    private String more_title;

    private int DATA_MODE;
    public static final String DATA_MODE_= "DATA_MODE_";
    public static final int DATA_MODE_CARTOON = 1;//漫画页面推广活动
    public static final int DATA_MODE_CP = 2;//Cp 页面征文活动
    public static final String MORE_URL = "more_url";
    public static final String MORE_TITLE = "more_title";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_banner_layout);

        initView();
        initData();
        rxData();
    }

    private void initData() {
        DATA_MODE = getIntent().getIntExtra(DATA_MODE_, DATA_MODE_CP);
        more_url = getIntent().getStringExtra(MORE_URL);
        more_title = getIntent().getStringExtra(MORE_TITLE);
        if (more_title != null) {
            mTitle.setText(more_title);
        }
        ApiClient apiClient = new ApiClient();
        more_url = more_url.replace("http://api.doufu.la/", "");
        apiClient.init(this);
        cpService = apiClient.getService();
    }

    private void rxData() {
        if (DATA_MODE == DATA_MODE_CP) {
            rxCpData();
        }else if (DATA_MODE == DATA_MODE_CARTOON) {
            rxCartoonData();
        }
    }

    /**
     * 漫画推广活动数据
     */
    private void rxCartoonData() {
        cpService.getMoreBannerCartoon(more_url)
                .compose(RxJavaHelper.<ResponseMoreBannerCartoonBean>observeOnMainThread(this))
                .subscribe(new Observer<ResponseMoreBannerCartoonBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        DoufuUtils.netWorkErrorToast(MoreBannerActivity.this);
                    }

                    @Override
                    public void onNext(ResponseMoreBannerCartoonBean responseMoreBannerBean) {
                        Log.d(TAG, "onNext: ");
                        setAdapter(responseMoreBannerBean.getData());
                    }
                });
    }

    /**
     * cp 征文活动数据
     */
    private void rxCpData() {
        cpService.getMoreBanner(more_url)
                .compose(RxJavaHelper.<ResponseMoreBannerBean>observeOnMainThread(this))
                .subscribe(new Observer<ResponseMoreBannerBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        DoufuUtils.netWorkErrorToast(MoreBannerActivity.this);
                    }

                    @Override
                    public void onNext(ResponseMoreBannerBean responseMoreBannerBean) {
                        setAdapter(responseMoreBannerBean.getData().getList());
                    }
                });
    }

    private void setAdapter(List<DFHomeBannerBean> dFHomeBannerBeanList) {
        mDFHomeBannerBeanList = dFHomeBannerBeanList;//for onclick event
        moreBannerAdapter = new MoreBannerAdapter(dFHomeBannerBeanList);
        GridLayoutManager mg = new GridLayoutManager(this, 2);
        mMoreBannerList.setLayoutManager(mg);
        mMoreBannerList.setAdapter(moreBannerAdapter);
        initEvent();
    }

    private void initEvent() {
        ItemClickSupport.addTo(mMoreBannerList).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                AppUtil.intentBanner(MoreBannerActivity.this, mDFHomeBannerBeanList.get(position));
            }
        });
    }

    private void initView() {
        mMoreBannerList = (RecyclerView) findViewById(R.id.more_banner_list);
        mTitle = (TextView) findViewById(R.id.personal_note_title_text);
    }
}
